var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
const apiserverUrl = require('../../config').apiserverUrl;
const ExamListUrl = require('../../config').ExamListUrl;
const ChapterListUrl = require('../../config').ChapterListUrl;
const CheckExamEnableUrl = require('../../config').CheckExamEnableUrl;
const CheckUserInExamUrl = require('../../config').CheckUserInExamUrl;
var app = getApp();
Page({

    data: {
        tabs: ["模拟练习", "章节练习"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        traininglist: [],
        chapterlist: []

    },
    onLoad: function () {
        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
        this.gettraininglist();
        this.getchapterlist();

    },
    trainClick: function (e) {
      var papersid = e.currentTarget.dataset.papersid;
      wx.request({
        method: "post",//请求数据用get，添加数据post，修改数据put
        url: apiserverUrl,
        data: {
          method: "GET",
          url: CheckExamEnableUrl,
          data: {
            papersid: papersid,
            userid: app.globalData.userInfo.userid
          }
        },
        header: {
          "Content-Type": "application/json"
        },
        success: res => {
          if (res.data.IsSuccess) {
            if (res.data.Data == 1) {
              wx.showModal({
                content: '考试进行中',
                showCancel: false
              });
            } else {
              wx.navigateTo({
                url: '../exercise/exercise?key=' + papersid + '&type=training',
              });
             
            } 
          
          }
        }
      });
    },
    chapterClick:function(e){
      var chapterid = e.currentTarget.dataset.chapterid;
      wx.request({
        method: "post",//请求数据用get，添加数据post，修改数据put
        url: apiserverUrl,
        data: {
          method: "GET", url: CheckUserInExamUrl, data: {
            userid: app.globalData.userInfo.userid
          }
        },
        header: {
          "Content-Type": "application/json"
        },
        success: res => {
          if (res.data.IsSuccess) {
              if (res.data.Data) {
                wx.showModal({
                  content: '考试中不能进行练习',
                  showCancel: false
                });
              } else {
            wx.navigateTo({
              url: '../exercise/exercise?key=' + chapterid+'&type=chapter',
            });
              }
          }
        }
      });
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    gettraininglist: function () {
        wx.request({
            method: "post",//请求数据用get，添加数据post，修改数据put
            url: apiserverUrl,
            data: {
                method: "GET", url: ExamListUrl, data: {}
            },
            header: {
                "Content-Type": "application/json"
            },
            success: res => {
                if (res.data.IsSuccess) {
                    this.setData({ traininglist: res.data.Data });
                }
            }
        });
    },
    getchapterlist: function () {
        wx.request({
            method: "post",//请求数据用get，添加数据post，修改数据put
            url: apiserverUrl,
            data: {
                method: "GET", url: ChapterListUrl, data: {}
            },
            header: {
                "Content-Type": "application/json"
            },
            success: res => {
                if (res.data.IsSuccess) {
                    this.setData({ chapterlist: res.data.Data });
                }
            }
        });
    }
})